package com.ms.controller.car;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ms.dao.entity.CarinformationEty;
import com.ms.dao.mapper.CarinformationDao;
import com.ms.util.EntityReflect;
import com.ms.util.ExtLimit;
import com.ms.util.JSONGrid;

@Controller
@RequestMapping(value="/car/CarInformationController/")
public class CarInformationController {
	
	private Logger logger = Logger.getLogger(CarInformationController.class);
	
	@Autowired
	private CarinformationDao carinformationDao;

	@RequestMapping(value="search.action")
	public @ResponseBody String search(HttpServletRequest request) throws Exception {
		CarinformationEty searchEty = (CarinformationEty) EntityReflect.createObjectFromRequest(request, CarinformationEty.class);
		ExtLimit limit = (ExtLimit) EntityReflect.createObjectFromRequest(request, ExtLimit.class);
		
		searchEty.setExtLimit(limit);
		int count = carinformationDao.selectLimitCount(searchEty);
		List<CarinformationEty> data = carinformationDao.selectByLimit(searchEty);
		
		return JSONGrid.toJSon(data, count).toString();
	}
	
	@RequestMapping(value="searchOnWayCars.action")
	public @ResponseBody String searchOnWayCars(HttpServletRequest request) {
		String node = request.getParameter("node");
		JSONArray retArr = new JSONArray();
		ExtLimit limit = (ExtLimit) EntityReflect.createObjectFromRequest(request, ExtLimit.class);
		CarinformationEty searchEty = (CarinformationEty) EntityReflect.createObjectFromRequest(request, CarinformationEty.class);
		searchEty.setExtLimit(limit);
		
		if(node.equals("root")) {
			List<CarData> dataList = carinformationDao.searchOnWayCars(searchEty); 
			for(CarData carData : dataList) {
				JSONObject aObj = new JSONObject();
				aObj.put("text", carData.getAbbreviation() + ", " + carData.getThecolor() + ", " + carData.getCount() + "辆");
				aObj.put("leaf", false);
				aObj.put("data", carData);
				retArr.add(aObj);
			}
		}
		else {
			searchEty.setAbbreviation(node);
			List<CarinformationEty> dataList = carinformationDao.selectByEntity(searchEty);
			for(CarinformationEty carData : dataList) {
				JSONObject aObj = new JSONObject();
				aObj.put("text", carData.getCarNO());
				aObj.put("leaf", true);
				aObj.put("data", carData);
				retArr.add(aObj);
			}
		}
		
		JSONObject retObj = new JSONObject();
		
		retObj.put("text", "Cars");
		retObj.put("items", retArr);
		return retObj.toString();
	}
	
	@ExceptionHandler
	public @ResponseBody String handle(Exception e) {
		logger.error(e.getMessage(), e);
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		obj.put("result","error");
		obj.put("info",e.getMessage());
		return obj.toString();
	}
}
